package com.example.daxiguaforharmony.game.core.loader;

import com.example.daxiguaforharmony.game.core.GameError;
import com.example.daxiguaforharmony.game.core.utils.Log;

public class GameLoadStep {
    protected String _stepName = "GameLoadStep";

    private GameLoader _gameLoader = null;
    private int _loadIndex = -1;
    private float _proportion = 10;
    private boolean _complete = false;

    /**
     * proportion：进度占比-百分制
     * @param proportion
     */
    public GameLoadStep(float proportion) {
        this._proportion = proportion;
    }

    /**
     * 需要重写此方法，开始加载
     */
    public void execute() {
        this._complete = false;
    }

    public void setGameLoader(GameLoader gameLoader) {
        this._gameLoader = gameLoader;
    }

    public void setLoadIndex(int index) {
        this._loadIndex = index;
    }

    public int getLoadIndex() {
        return this._loadIndex;
    }

    public String getStepName() {
        return this._stepName;
    }

    public boolean isExecuteComplete() {
        return this._complete;
    }

    public void destory() {
        this.onDestory();
    }

    protected void onStepProgress(float progress) {
        if(this._gameLoader != null) {
            float p1 = progress / 100;
            this._gameLoader.onStepProgress(this._proportion * p1);
        }
    }

    protected void onStepCompleted() {
        if (this._complete == true) {
            Log.error("GameLoadStep: step is completed! step=" + this._loadIndex);
        } else if(this._gameLoader != null) {
            this._complete = true;
            this._gameLoader.onStepCompleted(this._proportion);
        }
    }

    protected void onStepFail(GameError error) {
        if(this._gameLoader != null) {
            this._complete = true;
            this._gameLoader.onStepFail(error);
        }
    }

    protected void onDestory() {

    }
}
